//
//  ActivateViewController.m
//  JianChaRiBao
//
//  Created by yuzhou on 13-11-26.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "ActivateViewController.h"
#import "SDImageCache.h"

@interface ActivateViewController ()
{
    IBOutlet UIView *inputView;
    IBOutlet UIView *activatedView;
}
@end

@implementation ActivateViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stateChanged:) name:@"ActivateStateChanged" object:nil];
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reviewStatus) name:@"ReviewStatus" object:nil];
//        ReviewStatus
//        [self updateWithSilent:YES];
    }
    return self;
}
- (void)changeView{
    NSLog(@"isActivated:%d   isReview:%d",[ActivateManager shareManager].isActivated,[ActivateManager shareManager].isReview);
    if ([ActivateManager shareManager].isActivated && [ActivateManager shareManager].isReview) {
        activatedView.hidden = NO;
        inputView.hidden = YES;
    }else{
        activatedView.hidden = YES;
        inputView.hidden = NO;
    }

}
- (void)stateChanged:(NSNotification *)noti{
    [self changeView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSDictionary *infoDict = [[NSBundle mainBundle]infoDictionary];
    NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
    currentVersionLabel.text = [NSString stringWithFormat:@"当前版本：%@",currentVersion];
    cuttentVersion.text = [NSString stringWithFormat:@"当前版本：%@",currentVersion];
    [self reviewStatus];
}
-(void)reviewStatus{
    [self changeView];
    if ([ActivateManager shareManager].isReview) {
        activeView.hidden = NO;
    }else{
        activeView.hidden = YES;
    }
}

- (BOOL)inspectString:(NSString *)text{
    if (text == nil || [text isEqualToString:@""]) {
        ABPromptView * abp = [[ABPromptView alloc]initWithTitle:@"请输入激活码"];
        [abp show];
        [abp release];
        return NO;
    }else if (text.length != 8){
        ABPromptView * abp = [[ABPromptView alloc]initWithTitle:@"验证码非法"];
        [abp show];
        [abp release];
        return NO;
    }else{
        return YES;
    }
    


}
-(void)activateUser{
    [self.activateNumTextField resignFirstResponder];
    if ([[[UIDevice currentDevice] systemVersion]floatValue] >= 6.0) {
        if ([self inspectString:self.activateNumTextField.text]) {
            [[ActivateManager shareManager] activateWith:self.activateNumTextField.text];
        }
    }
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self activateUser];
    [self.activateNumTextField resignFirstResponder];
    return YES;
}

- (IBAction)cancelButtonClicled:(id)sender {
    [self.activateNumTextField resignFirstResponder];
}
- (IBAction)activateButtonClicked:(id)sender {
    [self activateUser];
}
- (IBAction)deleteCacheButtonClicked:(id)sender {
    NSString *asiDownlaodCachePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"ASIHTTPRequestCache"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:asiDownlaodCachePath]) {
        [[NSFileManager defaultManager] removeItemAtPath:asiDownlaodCachePath error:nil];
    }
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
    ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"缓存已清除"] autorelease];
    [promptView show];
}

- (IBAction)updateButtonClicked:(id)sender {
    cuttentVersion.textColor = [UIColor blackColor];
    currentVersionLabel.textColor = [UIColor blackColor];
//    [self updateWithSilent:NO];
    
    [[ConvenientTool sharedConvenienTool ] ChackAppVersionNeedPopAlert:YES];
    
    
}

- (IBAction)updateButtonTouchDown:(id)sender {
    cuttentVersion.textColor = [UIColor darkGrayColor];
    currentVersionLabel.textColor = [UIColor darkGrayColor];
}
- (IBAction)updateButtonTouchUpOutside:(id)sender {
    cuttentVersion.textColor = [UIColor blackColor];
    currentVersionLabel.textColor = [UIColor blackColor];
}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//    NSLog(@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:EndTime]);
    [self.activateNumTextField resignFirstResponder];
}

-(void)updateWithSilent:(BOOL)silent{
//    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"请稍候" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
//    [alert show];
//    [alert release];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        BOOL haveNewUpdate = [[ConvenientTool sharedConvenienTool] haveNewUpdate];
        dispatch_async(dispatch_get_main_queue(), ^{
//            [alert dismissWithClickedButtonIndex:0 animated:YES];
            if (!haveNewUpdate) {
                if (silent) {
                    return ;
                }
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已是最新版本" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                [alert release];
            }else{
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"有新版本是否更新" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
                [alert show];
                [alert release];
            }
        });
    });
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [[ConvenientTool sharedConvenienTool] showAppInfo];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [inputView release];
    [activatedView release];
    [cuttentVersion release];
    [self.activateNumTextField release];
    [currentVersionLabel release];
    [activeView release];
    [super dealloc];
}
@end
